﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using AChance.Func;

namespace AChance.Dal.Func
{
    public static class DalFileBase
    {
        public static class Name
        {
            public const string LOCAL = "Local";
        }
        public enum Type
        {
            Local,
            Share,
            Ftp
        }

        private static ConcurrentDictionary<string, IFileOper> _fileOper = new ConcurrentDictionary<string, IFileOper>();
        /// <summary>
        /// 获取文件库的操作实例
        /// </summary>
        internal static IFileOper GetFileOper(string connectName)
        {
            return _fileOper.GetOrAdd(connectName, CreateFileOper(connectName));
        }
        /// <summary>
        /// 创建文件库的操作实例
        /// </summary>
        internal static IFileOper CreateFileOper(string connectName)
        {
            return CreateFileOper(connectName, FunContext.AppSettings.Connect.FileStrings[connectName]);
        }
        internal static IFileOper CreateFileOper(string connectName, string connectString)
        {
            if (connectName.Contains(Type.Local.ToString()))
                return new LocalFileOper(connectString);
            //else if (connectName.Contains(Type.Share.ToString()))
            //    return new ShareFileOper(connectString);
            //else if (connectName.Contains(Type.Ftp.ToString()))
            //    return new FtpFileOper(connectString);
            else
                return null;
        }

        /// <summary>
        /// 文件库操作接口
        /// </summary>
        public interface IFileOper
        {
            bool Connect();
            bool ExistsFile(string remoteFileName);
            bool UploadFile(string localFilePath);
            bool DownloadFile(string remoteFileName, string localFilePath);
            bool DeleteFile(string remoteFileName);
        }
        /// <summary>
        /// 本地文件库操作
        /// </summary>
        private class LocalFileOper : IFileOper
        {
            public LocalFileOper(string config)
            {
                string[] aryConfig = config.Split(';');
                foreach (string item in aryConfig)
                {
                    if (!item.Contains("="))
                        continue;
                    string strKey = item.Split('=')[0].Trim().ToUpper();
                    string strValue = item.Split('=')[1].Trim();
                    if (strKey.Equals(nameof(Address).ToUpper()))
                        Address = strValue.EndsWith("\\") ? strValue : strValue + "\\";
                }
            }
            /// <summary>
            /// 目录地址
            /// </summary>
            private string Address { get; set; }
            /// <summary>
            /// 连接
            /// </summary>
            public bool Connect()
            {
                return true;
            }
            /// <summary>
            /// 存在文件
            /// </summary>
            public bool ExistsFile(string remoteFileName)
            {
                return true;
            }
            /// <summary>
            /// 上传文件
            /// </summary>
            public bool UploadFile(string localFilePath)
            {
                return true;
            }
            /// <summary>
            /// 下载文件
            /// </summary>
            public bool DownloadFile(string remoteFileName, string localFilePath)
            {
                return true;
            }
            /// <summary>
            /// 删除文件
            /// </summary>
            public bool DeleteFile(string remoteFileName)
            {
                return true;
            }
        }
        /// <summary>
        /// 共享文件库操作
        /// </summary>
        //private class ShareFileOper : IFileOper
        //{
        //    public ShareFileOper(string config)
        //    {

        //    }
        //    /// <summary>
        //    /// 目录地址
        //    /// </summary>
        //    public string Address { get; set; }
        //    /// <summary>
        //    /// 用户账号
        //    /// </summary>
        //    public string UserId { get; set; }
        //    /// <summary>
        //    /// 用户密码
        //    /// </summary>
        //    public string Password { get; set; }
        //    /// <summary>
        //    /// 连接
        //    /// </summary>
        //    public void Connect()
        //    {

        //    }
        //    /// <summary>
        //    /// 存在文件
        //    /// </summary>
        //    public void ExistsFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 上传文件
        //    /// </summary>
        //    public void UploadFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 下载文件
        //    /// </summary>
        //    public void DownloadFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 删除文件
        //    /// </summary>
        //    public void DeleteFile()
        //    {

        //    }
        //}
        /// <summary>
        /// Ftp文件库操作
        /// </summary>
        //private class FtpFileOper : IFileOper
        //{
        //    public FtpFileOper(string config)
        //    {

        //    }
        //    /// <summary>
        //    /// 目录地址
        //    /// </summary>
        //    public string Address { get; set; }
        //    /// <summary>
        //    /// 用户账号
        //    /// </summary>
        //    public string UserId { get; set; }
        //    /// <summary>
        //    /// 用户密码
        //    /// </summary>
        //    public string Password { get; set; }
        //    /// <summary>
        //    /// 连接
        //    /// </summary>
        //    public void Connect()
        //    {

        //    }
        //    /// <summary>
        //    /// 存在文件
        //    /// </summary>
        //    public void ExistsFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 上传文件
        //    /// </summary>
        //    public void UploadFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 下载文件
        //    /// </summary>
        //    public void DownloadFile()
        //    {

        //    }
        //    /// <summary>
        //    /// 删除文件
        //    /// </summary>
        //    public void DeleteFile()
        //    {

        //    }
        //}
    }
}
